<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="题目标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="题目标题"></el-input>
      </el-form-item>
      <el-form-item label="题目解答" prop="answer">
        <el-input type="textarea" v-model="dataForm.answer" placeholder="题目解答"></el-input>
      </el-form-item>
      <el-form-item label="题目难度等级" prop="level">
        <el-input
          v-model="dataForm.level"
          placeholder="题目难度等级"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="displayOrder">
        <el-input v-model="dataForm.displayOrder" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="副标题" prop="subTitle">
        <el-input v-model="dataForm.subTitle" placeholder="副标题"></el-input>
      </el-form-item>
      <el-form-item label="题目类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
             <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.type"
            :value="item.id"
          >
               </el-option
          >
             </el-select
        >
      </el-form-item>
      <el-form-item label="是否显示" prop="enable">
        <el-radio v-model="dataForm.enable" label="0">不显示</el-radio>
            <el-radio v-model="dataForm.enable" label="1">显示</el-radio>
      </el-form-item>
      <el-form-item label="删除标记" prop="delFlag">
        <el-radio v-model="dataForm.delFlag" label="0">不删除</el-radio>
           <el-radio v-model="dataForm.delFlag" label="1">删除</el-radio>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <div class="block">
          <el-date-picker
            v-model="dataForm.createTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </div>
        <!-- <el-input
          v-model="dataForm.createTime"
          placeholder="创建时间"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <div class="block">
          <el-date-picker
            v-model="dataForm.updateTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </div>
        <!-- <el-input
          v-model="dataForm.updateTime"
          placeholder="更新时间"
        ></el-input> -->
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options: [],
      visible: false,
      dataForm: {
        id: 0,
        title: "",
        answer: "",
        level: "",
        displayOrder: "",
        subTitle: "",
        type: "",
        enable: "1",
        delFlag: "0",
        createTime: "",
        updateTime: "",
      },
      dataRule: {
        title: [
          { required: true, message: "题目标题不能为空", trigger: "blur" },
        ],
        answer: [
          { required: true, message: "题目解答不能为空", trigger: "blur" },
        ],
        level: [
          { required: true, message: "题目难度等级不能为空", trigger: "blur" },
        ],
        displayOrder: [
          {
            validator: (rule, value, callback) => {
              if (value == "") {
                callback(new Error("排序字段必须填写"));
              } else if (!(value % 1 === 0) || value < 0) {
                callback(new Error("排序必须是一个大于等于0的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        subTitle: [
          { required: true, message: "副标题不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "题目类型不能为空", trigger: "blur" },
        ],
        enable: [
          { required: true, message: "是否显示不能为空", trigger: "blur" },
        ],
        delFlag: [
          {
            required: true,
            message: "删除标记（0-正常，1-删除）不能为空",
            trigger: "blur",
          },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/question/question/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.question;
              this.dataForm.enable = data.question.enable + "";
              this.dataForm.delFlag = data.question.delFlag + "";
            }
          });
        }
      });
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.$http({
          url: this.$http.adornUrl(
              `/question/type/list?page=1&limit=1000&key=`
            ),
            method: "post",
            params: this.$http.adornParams() 
        }).then(({data})=>{
          this.options = data.page.list
        })
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/question/question/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              title: this.dataForm.title,
              answer: this.dataForm.answer,
              level: this.dataForm.level,
              displayOrder: this.dataForm.displayOrder,
              subTitle: this.dataForm.subTitle,
              type: this.dataForm.type,
              enable: this.dataForm.enable,
              delFlag: this.dataForm.delFlag,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
